import Vue from 'vue'
import VueRouter from 'vue-router'
import {getToken} from "@/plugins/token";

Vue.use(VueRouter)

//路由组件
export const routes= [
    /*
    * {
    * name:"",
    * component:()=>{},
    * path:"",
    * mate:{
    * title:"",
    * icon:""
    * }
    * }*/
    //主页
    {
        name:"Home",
        component:()=>import("@/pages/home"),
        path:'/home',
        mate:{
            title:"主页",
            icon:"",
        }
    },
    {
      name:'Payout',
      component:()=>import("@/pages/payout"),
        path:'/payout',
        mate:{
          title:"支出",
            icon:'',
        }
    },
    {
      name:'Income',
      component:()=>import("@/pages/income"),
      path:'/income',
      mate:{
          title:'收入',
          icon:""
      }
    },
    //登录页
    {
        name:"Login",
        component:()=>import("@/pages/login/index"),
        // component:Login,
        path:'/login',
        mate:{
            title:"",
            icon:""
        }
    },
    //注册页
    {
      name: "Register",
      path:'/register',
      component:()=>import("@/pages/register/index"),
      mate:{
        title:"",
        icon:""
      }
    },
    //404
    {
        name:'404',
        path:'*',
        component:()=>import("@/pages/404/404.vue")
    },
    //设置重定向，当项目跑起来时自动定向访问home页面，
    {
        path: '*',
        redirect: '/home'
    }
]

//路由器
const router=new VueRouter({
    mode:'history',//history路径值
    routes: routes,
})

//路由守卫
router.beforeEach((to,from,next)=>{
    //本地存储的用户信息
    let userName=getToken();
    if(userName==undefined){
        if(to.path=="/login"||to.path=="/register"){
            next();
        }else{
            next("/login")
        }
    }else{
        next();
    }
})
export default router